package com.jit.BluetoothKey.fragment.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Administrator on 2018/1/25.
 * 基类 Fragment
 */

public abstract class BaseFragment extends Fragment {

    protected Activity mActivity;   //全局使用的Activity
    protected View mRootView;   //布局View

    /**
     * 获取用于全局使用的Activity
     *
     * @param context
     */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(getLayoutId(), container, false);
        EventBus.getDefault().register(this);
        init();
        initCondition();
        initView();
        initData();
        initAdapt();
        initListner();
        return mRootView;
    }

    /**
     * 初始化监听事件
     */
    protected void initListner() {
    }

    /**
     * 初始化适配器
     */
    protected void initAdapt() {
    }

    /**
     * 初始化Data
     */
    protected void initData() {
    }

    /**
     * 初始化View
     */
    protected void initView() {
    }

    /**
     * 初始化请求参数
     */
    protected void initCondition() {
    }

    /**
     * 保留作最开始的初始化
     */
    protected void init() {
    }


    /**
     * 用于子类实现 返回layout ID
     *
     * @return
     */
    protected abstract int getLayoutId();


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void pub(String event){

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //EventBus 注销
        EventBus.getDefault().unregister(this);
    }
}
